package com.example.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.domain.AddressBook;
import com.example.mapper.AddressBookMapper;
import com.example.service.IAddressBookService;
import com.example.service.ex.DeleteException;
import com.example.service.ex.InsertException;
import com.example.service.ex.UpdateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 地址业务
 */
@Service
public class AddressBookServiceImpl implements IAddressBookService {
    @Autowired
    private AddressBookMapper addressBookMapper;
    @Override
    public List<AddressBook> list(Long id) {
        //创建条件构造器
        LambdaQueryWrapper<AddressBook> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AddressBook::getUserId,id);
        lambdaQueryWrapper.orderByDesc(AddressBook::getIsDefault).orderByDesc(AddressBook::getUpdateTime);
        //查询该用户的外卖地址信息
        List<AddressBook> addressBooks = addressBookMapper.selectList(lambdaQueryWrapper);
        return addressBooks;
    }

    @Override
    public void IsDefault(AddressBook addressBook,Long id) {
        //把所有该用户的地址设置成不默认
        Integer update = addressBookMapper.update(id);
        if (update<=0){
            throw new UpdateException("更新数据产生未知的异常");
        }
        //根据信息修改成默认的地址信息
        AddressBook addressBook1 = addressBookMapper.selectById(addressBook.getId());
        //将查询到的对象设置成默认地址
        addressBook1.setIsDefault(1);
        //将数据库中的数据进行修改
        int i = addressBookMapper.updateById(addressBook1);
        if (i<=0){
            throw new UpdateException("更新数据产生未知的异常");
        }
    }
    //添加用户
    @Override
    public void save(AddressBook addressBook, Long id) {
        addressBook.setUserId(id);
        //查询数据库中有没有地址信息
        LambdaQueryWrapper<AddressBook> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AddressBook::getUserId,id);
        Integer count = addressBookMapper.selectCount(lambdaQueryWrapper);
        //判断如果没有数据将其设置成默认地址
        if (count<1||count==null){
            addressBook.setIsDefault(1);
        }
        //将地址添加到数据库中
        int insert = addressBookMapper.insert(addressBook);
        if (insert<1){
            throw new InsertException("添加数据时产生未知的异常");
        }
    }

    @Override
    public AddressBook getById(Long id,Long userId) {
        //根据传递的id地址信息
        LambdaQueryWrapper<AddressBook> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AddressBook::getId,id).eq(AddressBook::getUserId,userId);
        AddressBook addressBook = addressBookMapper.selectOne(lambdaQueryWrapper);
        return addressBook;
    }

    @Override
    public void update(AddressBook addressBook) {
        //查询要修改的地址信息是否存在
        AddressBook addressBook1 = addressBookMapper.selectById(addressBook.getId());
        if (addressBook1==null){
            throw new UpdateException("您要修改的信息不存在");
        }
        int i = addressBookMapper.updateById(addressBook);
        if (i<=0){
            throw new UpdateException("在更新数据中产生未知的异常");
        }
    }

    @Override
    public void delete(Long id,Long userId) {
        //创建条件构造器
        LambdaQueryWrapper<AddressBook> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AddressBook::getId,id).eq(AddressBook::getUserId,userId);
        //查询语句
        AddressBook addressBook = addressBookMapper.selectOne(lambdaQueryWrapper);
        if (addressBook==null){
            throw new DeleteException("地址不存在");
        }
        int i = addressBookMapper.deleteById(id);
        if (i<=0) {
            throw new DeleteException("删除地址时出现未知的异常");
        }
        LambdaQueryWrapper<AddressBook> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.eq(AddressBook::getUserId,userId);
        //查询数据库中是否还有地址信息，如果没有直接返回
        Integer count = addressBookMapper.selectCount(lambdaQueryWrapper1);
        if (count==0){
            return;
        }
        //这里的获取的值是上面查询出的值进行的判断，不必在意上面做完删除操作会没有数据，因为这个对象已经查询出来存储到了内存中
        //这里判断删除的是不是默认地址，如果是就将后面的数据变成默认地址
        if (addressBook.getIsDefault()==1){
            AddressBook addressBook1 = addressBookMapper.selectByUid(userId);
            addressBook1.setIsDefault(1);
            int i1 = addressBookMapper.updateById(addressBook1);
            if (i1<=0){
                throw new UpdateException("更新数据时产生未知的异常");
            }
        }
    }
    //查询用户的默认地址信息
    @Override
    public AddressBook defalt(Long id) {
        //创建条件构造器
        LambdaQueryWrapper<AddressBook> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //添加条件
        lambdaQueryWrapper.eq(id!=null,AddressBook::getUserId,id);
        lambdaQueryWrapper.eq(AddressBook::getIsDefault,1);
        AddressBook addressBook = addressBookMapper.selectOne(lambdaQueryWrapper);
        return addressBook;
    }
}
